1 00:00:00,350 --> 00:00:01,190 Hey there. 2 00:00:01,190 --> 00:00:05,870 In this lecture, we're going to continue using object oriented programming to create land mines. 3 00:00:05,870 --> 00:00:08,330 We'll be using the concept of composition. 4 00:00:08,330 --> 00:00:12,230 And we'll also be using the collection service to create the land mine you see here. 5 00:00:12,230 --> 00:00:13,970 So let's go ahead and playtest our game. 6 00:00:13,970 --> 00:00:16,310 And I'll demonstrate to you what our land mine does. 7 00:00:16,760 --> 00:00:19,010 So we can go and walk over to our land mine. 8 00:00:19,010 --> 00:00:20,930 And once we touch it boom. 9 00:00:20,930 --> 00:00:22,400 We blow up the land. 10 00:00:22,400 --> 00:00:25,340 Mine disappears and we've taken some damage. 11 00:00:26,320 --> 00:00:31,300 Now, before we begin, you'll have to grab this landmine model that I've attached to the lecture. 12 00:00:31,300 --> 00:00:34,540 And this model simply contains a module script. 13 00:00:34,540 --> 00:00:39,790 And inside this module script we have some basic properties, such as the properties we'll have for 14 00:00:39,790 --> 00:00:45,610 our explosion, as well as how much damage the explosion does per hit and the weight sensitivity for 15 00:00:45,610 --> 00:00:46,510 our landmine. 16 00:00:47,330 --> 00:00:49,490 You're also going to need a plugin. 17 00:00:49,490 --> 00:00:54,260 And this plugin is called tag editor or instance tagging. 18 00:00:54,260 --> 00:00:56,360 And I've also attached this plugin to the lecture. 19 00:00:56,360 --> 00:00:57,680 So you can go ahead and grab that. 20 00:00:57,680 --> 00:01:01,850 So you can modify the tags of parts and instances in your game. 21 00:01:02,550 --> 00:01:07,710 So once you have this plugin downloaded and installed in studio, you can go ahead and open it up here. 22 00:01:07,710 --> 00:01:12,390 And what this plugin will allow you to do is to create a new tag that you can attach to instances in 23 00:01:12,390 --> 00:01:13,050 your game. 24 00:01:13,050 --> 00:01:15,030 In this case, we're going to create a new tag. 25 00:01:15,030 --> 00:01:16,560 I'm going to call it landmine. 26 00:01:16,830 --> 00:01:20,340 And using this tag we can now attach it to objects in our game. 27 00:01:20,340 --> 00:01:27,060 So for example if I select this landmine and then I select my part, you'll see this checkbox appear. 28 00:01:27,060 --> 00:01:32,460 And when I select this checkbox that means this instance now has this landmine tag attached to it. 29 00:01:32,460 --> 00:01:38,460 So if I go and look under tagged instances here, my landmine shows up in this list. 30 00:01:39,360 --> 00:01:44,790 And because that our land mine is now tagged, we can use the collection service to gather every single 31 00:01:44,790 --> 00:01:47,490 instance in our game that has this land mine tag. 32 00:01:47,490 --> 00:01:52,410 And it doesn't matter where our instance is stored, it could be in a folder and a model. 33 00:01:52,410 --> 00:01:53,820 It could be in a different service. 34 00:01:53,820 --> 00:01:58,590 But because it has this land mine tag attached to it, we can go ahead and collect all of them. 35 00:01:58,590 --> 00:02:01,620 And that's the very useful thing about the collection service. 36 00:02:02,190 --> 00:02:06,480 So once you have this tag added to your land mine, we can go ahead and start getting scripting. 37 00:02:07,240 --> 00:02:09,130 Inside of this module script I've created. 38 00:02:09,130 --> 00:02:13,150 I've just called it landmine, and this module script is going to act as my class. 39 00:02:13,150 --> 00:02:16,000 And in here we're going to need a few things. 40 00:02:16,000 --> 00:02:18,220 Of course we're going to need some services. 41 00:02:18,250 --> 00:02:19,540 Nothing too crazy. 42 00:02:19,540 --> 00:02:23,380 The first thing we'll need is the server storage service. 43 00:02:23,380 --> 00:02:28,510 And the reason I'm getting this is because my classes are stored in this service. 44 00:02:28,510 --> 00:02:30,280 So we're going to head and get that. 45 00:02:30,280 --> 00:02:33,100 And we're also going to get the collection service. 46 00:02:35,780 --> 00:02:38,780 Election service, and we're going to be using that in a moment. 47 00:02:40,030 --> 00:02:45,820 Now we're also going to need some variables to set up our class again to uphold encapsulation, I'm 48 00:02:45,820 --> 00:02:47,800 going to create two different tables. 49 00:02:47,800 --> 00:02:49,210 One I'll just call landmine. 50 00:02:49,210 --> 00:02:52,960 And this will act as a table to store my constructor. 51 00:02:52,960 --> 00:02:56,980 And then I'm going to create another one I'll call it MT short for meta table. 52 00:02:56,980 --> 00:03:00,820 And it's going to store all the functions for the objects of this class. 53 00:03:00,820 --> 00:03:03,940 And these objects are going to have quite a few properties. 54 00:03:03,940 --> 00:03:08,860 One is going to be the part or the landmine that is representing this object. 55 00:03:09,190 --> 00:03:13,150 We'll have a property for whether or not it's enabled, and we'll set it to true by default. 56 00:03:13,270 --> 00:03:20,320 We'll have a condition to check whether or not it has been detonated, detonated set it to false. 57 00:03:21,070 --> 00:03:26,260 We're going to create a property for the properties module script in our landmine, call it properties 58 00:03:26,260 --> 00:03:27,730 and just set it to null for now. 59 00:03:27,820 --> 00:03:31,810 And we'll have a property for the explosive. 60 00:03:31,810 --> 00:03:35,920 And you'll see what I mean in a minute because we're going to create another class. 61 00:03:35,920 --> 00:03:38,290 And this class is going to represent an explosion. 62 00:03:38,290 --> 00:03:43,330 And we're going to take an object from that class and store it within our landmine object. 63 00:03:43,330 --> 00:03:46,300 So we're upholding this idea of composition. 64 00:03:47,060 --> 00:03:53,540 After that, we can set the landmine meta tables underscore underscore index meta method to itself. 65 00:03:53,540 --> 00:03:56,750 That way we can access all these properties and functions. 66 00:03:57,970 --> 00:04:02,200 The next thing I'm going to do is I'm going to create a variable for this explosion class. 67 00:04:02,200 --> 00:04:03,730 I'll just call it explosion. 68 00:04:03,820 --> 00:04:10,630 And I'm going to require from my server storage in a folder called classes, where I have the explosion 69 00:04:10,630 --> 00:04:11,560 module script. 70 00:04:11,560 --> 00:04:13,570 And we're going to be filling this out in a little bit. 71 00:04:13,600 --> 00:04:17,080 Not currently, but we're going to continue setting up our landmine class. 72 00:04:17,080 --> 00:04:21,400 I'm going to have a table that's going to store all the current landmine objects. 73 00:04:22,680 --> 00:04:24,330 And I'm going to create a boolean here. 74 00:04:24,330 --> 00:04:27,450 I'll call it initialized set to false. 75 00:04:27,450 --> 00:04:31,830 But what I'm going to do is I'm going to create a function called init which is short for initialize. 76 00:04:31,830 --> 00:04:34,530 And it's going to go and set up everything for us. 77 00:04:34,530 --> 00:04:38,670 So it's going to use the collection service and collect all the landmines in our game and create objects 78 00:04:38,670 --> 00:04:39,210 for them. 79 00:04:39,210 --> 00:04:41,100 And then it'll set initialize to true. 80 00:04:41,100 --> 00:04:43,320 So we don't have to initialize this class anymore. 81 00:04:44,390 --> 00:04:46,880 And then I'm going to add one constant. 82 00:04:47,300 --> 00:04:51,860 And this constant is going to be for that tag we made I'll call it landmine tag. 83 00:04:51,860 --> 00:04:54,440 And it's just going to be a string that is landmine. 84 00:04:54,440 --> 00:04:59,120 So the exact same string as the one we created for our tag. 85 00:05:00,140 --> 00:05:06,410 Next, we're going to need a section for some private functions, because we need to abstract our class 86 00:05:06,410 --> 00:05:10,220 and hide any unimportant details we don't need to see on the outside. 87 00:05:10,610 --> 00:05:14,330 So one function I will call get total weight. 88 00:05:15,110 --> 00:05:17,000 And I'll pass a part to it. 89 00:05:17,000 --> 00:05:20,780 So what this function is going to do is that when our landmine gets touched, it's going to check the 90 00:05:20,780 --> 00:05:21,830 weight of that part. 91 00:05:21,830 --> 00:05:26,030 And if that part is a part of a model, then it'll check the entire weight of the model. 92 00:05:26,030 --> 00:05:30,470 That way if the weight reaches a specific threshold, then the bomb will blow up. 93 00:05:30,470 --> 00:05:33,620 But if it's something that's too light, then the bomb won't blow up at all. 94 00:05:34,100 --> 00:05:38,270 And then we're also going to have a function where when our landmine gets touched, we're going to call 95 00:05:38,270 --> 00:05:45,470 it on landmine touched, and we're going to have to pass the object as well as the other part that touched 96 00:05:45,470 --> 00:05:46,220 the landmine. 97 00:05:46,220 --> 00:05:47,870 We'll fill this out in a little bit. 98 00:05:48,390 --> 00:05:49,080 And the next. 99 00:05:49,080 --> 00:05:53,340 We need a section for some public functions like our constructor and things like that. 100 00:05:54,350 --> 00:05:58,130 So the first thing I'll create actually is the initialize function. 101 00:05:58,130 --> 00:06:02,150 I'm going to just call it init short for initialize. 102 00:06:02,390 --> 00:06:04,160 And we're going to check that boolean. 103 00:06:04,160 --> 00:06:08,120 So if we've already initialized this class then we're just going to return. 104 00:06:08,360 --> 00:06:10,820 Otherwise we're going to set initialize to true. 105 00:06:11,240 --> 00:06:18,140 And then what we can do is we can get all of the landmines in our game by using the collection service. 106 00:06:18,140 --> 00:06:23,780 And there's a function in the collection service called Get Tagged right here. 107 00:06:24,260 --> 00:06:27,680 And it returns to us an array of objects in the game with a given tag. 108 00:06:27,680 --> 00:06:29,210 So we can pass that string. 109 00:06:29,210 --> 00:06:34,460 We just made the landmine tag, and now every single instance in our game is going to be stored in a 110 00:06:34,460 --> 00:06:37,340 table that has the tag of landmine. 111 00:06:37,640 --> 00:06:42,230 Then we can go and we can loop through every single landmine that's in this table. 112 00:06:46,320 --> 00:06:50,940 And we can use a constructor function to create new landmine objects. 113 00:06:50,940 --> 00:06:52,650 So we'll actually make that right now. 114 00:06:52,650 --> 00:06:56,430 So function landmine dot new. 115 00:06:56,430 --> 00:06:57,750 So this is our constructor. 116 00:06:57,750 --> 00:06:59,820 We have to pass a part to this function. 117 00:06:59,820 --> 00:07:01,710 And I'll designate it as a part. 118 00:07:01,980 --> 00:07:04,110 And this part has to be our landmine. 119 00:07:04,110 --> 00:07:12,360 So we're going to create a table we'll call it self and attach the landmine meta table to this new table. 120 00:07:13,140 --> 00:07:16,920 And then from that point we have access to all those properties we made earlier. 121 00:07:16,920 --> 00:07:21,420 So we can set the part equal to the part pass to the function. 122 00:07:21,420 --> 00:07:26,760 We can set the properties equal to the parts property. 123 00:07:26,760 --> 00:07:30,150 So remember we have a module script in our landmine that we're going to require. 124 00:07:30,150 --> 00:07:32,370 So we can require part. 125 00:07:32,370 --> 00:07:35,160 And then the dot properties. 126 00:07:35,990 --> 00:07:38,480 So we get the properties for our landmine. 127 00:07:39,500 --> 00:07:44,330 And now we can set the self dot explosive equal to. 128 00:07:44,330 --> 00:07:46,610 We'll reference our explosion class. 129 00:07:46,610 --> 00:07:50,750 And in there we're also going to be creating a dot new function or a constructor. 130 00:07:50,750 --> 00:07:51,710 We haven't made one yet. 131 00:07:51,710 --> 00:07:53,390 So we're just going to leave this blank for now. 132 00:07:53,390 --> 00:07:55,460 But we'll fill it out in a little bit. 133 00:07:56,450 --> 00:08:03,140 And then I think an extra property will actually add to this table is a connection, and it's going 134 00:08:03,140 --> 00:08:04,820 to be connected to our part. 135 00:08:04,820 --> 00:08:08,480 And when it gets touched we're going to connect a function to it. 136 00:08:09,270 --> 00:08:14,910 And from there we can call that on landmine touch function and pass the object itself, as well as the 137 00:08:14,910 --> 00:08:19,320 other part that touched our part which is passed to this lambda function. 138 00:08:19,350 --> 00:08:21,990 Again, we're going to fill out this function in a little bit. 139 00:08:23,090 --> 00:08:27,410 And then once we do all that, then we can insert our object into that table. 140 00:08:27,410 --> 00:08:31,010 We created current landmines and insert self. 141 00:08:31,490 --> 00:08:35,300 And then finally we can return self back to where this function was called. 142 00:08:37,090 --> 00:08:42,370 Some other functions we're going to need will be now a part of our landmine meta table. 143 00:08:42,370 --> 00:08:45,040 So landmine empty. 144 00:08:45,310 --> 00:08:48,790 We're going to have a function to explode the landmine. 145 00:08:49,810 --> 00:08:56,650 We're also going to have a function to enable or disable our landmine so we can make it, uh, you know, 146 00:08:56,650 --> 00:08:59,050 hostile or docile. 147 00:08:59,590 --> 00:09:05,980 So we'll have one to enable it and we'll have one to disable it. 148 00:09:07,380 --> 00:09:10,920 Now let's say we also wanted to repair our landmine. 149 00:09:10,920 --> 00:09:12,360 So let's say it got detonated. 150 00:09:12,360 --> 00:09:19,740 And a little while later we wanted the landmine to respawn so we could create a function like landmine 151 00:09:19,740 --> 00:09:20,160 empty. 152 00:09:20,190 --> 00:09:21,810 We'll call it like repair. 153 00:09:21,810 --> 00:09:27,990 And in here we can repair the landmine back to its previous state so it can be ready for another detonation. 154 00:09:28,610 --> 00:09:30,260 So this seems pretty good so far. 155 00:09:30,260 --> 00:09:33,470 I think what we'll do is start filling out some of these functions. 156 00:09:33,470 --> 00:09:36,590 So what do we want to do to enable our landmine? 157 00:09:36,590 --> 00:09:40,430 Well we can just do self dot enabled equal to true. 158 00:09:40,430 --> 00:09:43,700 And in here we can just do self dot enabled equal to false. 159 00:09:43,700 --> 00:09:44,810 Pretty simple. 160 00:09:46,380 --> 00:09:53,100 So now that we've created our constructor, we can pass the new function here because it belongs to 161 00:09:53,100 --> 00:09:53,790 this table. 162 00:09:53,790 --> 00:09:59,220 Even though we created this function after this function was created, because it belongs to this table, 163 00:09:59,220 --> 00:10:03,420 and we're going to be calling this function later, we're able to access our constructor here. 164 00:10:03,900 --> 00:10:06,120 And then we can pass that landmined. 165 00:10:06,770 --> 00:10:09,920 To the function. 166 00:10:09,980 --> 00:10:15,230 So now if we call this function outside in a server script, all we need to do is call init. 167 00:10:15,230 --> 00:10:18,500 And every single landmine in our game will be set up and ready to go. 168 00:10:19,040 --> 00:10:21,650 Now another thing we can do is fill out the explode function. 169 00:10:21,650 --> 00:10:25,220 So what do we want to happen when this function gets called? 170 00:10:25,250 --> 00:10:26,870 Well we want to blow up the landmine right. 171 00:10:26,870 --> 00:10:33,470 But we first have to check a couple criteria such as these booleans we created up here. 172 00:10:33,470 --> 00:10:37,790 So for example if a landmine is disabled then we don't want to blow it up. 173 00:10:38,260 --> 00:10:40,930 So if not self dot enabled. 174 00:10:42,900 --> 00:10:50,220 Then we're going to print something like landmine is disabled and we'll just return end. 175 00:10:51,240 --> 00:10:55,020 And then we also need to check if this landmine has already been detonated. 176 00:10:55,020 --> 00:10:57,450 So self dot detonated. 177 00:10:57,870 --> 00:10:59,550 Then we'll print. 178 00:10:59,550 --> 00:11:04,710 Landmine already exploded and then we're going to return end. 179 00:11:04,860 --> 00:11:09,300 Otherwise we'll set self dot detonated to true because we're about to blow up the landmine. 180 00:11:11,170 --> 00:11:15,670 And from this point, this is where we would take this explosion object. 181 00:11:15,670 --> 00:11:20,140 We would create and call a function on it to blow up, you know, an explosion. 182 00:11:20,530 --> 00:11:22,660 So we'll make a note for that here. 183 00:11:22,660 --> 00:11:24,700 Blow up explosive. 184 00:11:24,700 --> 00:11:29,290 And after that explosive gets blown up, we can disable this landmine. 185 00:11:29,290 --> 00:11:37,390 Calling the disable function, we can set the part dot transparency transparency equal to one so the 186 00:11:37,390 --> 00:11:38,860 landmine is invisible. 187 00:11:38,860 --> 00:11:43,150 And then we could also do something like part dot can collide equal to false. 188 00:11:43,150 --> 00:11:46,030 So we can't collide with an invisible landmine. 189 00:11:46,510 --> 00:11:51,520 And then what we could do down here is that we could repair the landmine simply by re-enabling it. 190 00:11:52,190 --> 00:11:54,020 So we could do um. 191 00:11:54,020 --> 00:11:57,980 Self dot detonated equal to false. 192 00:11:59,320 --> 00:12:07,990 And then we can set the party transparency back to be opaque, and then we'll make sure to turn on can 193 00:12:07,990 --> 00:12:09,010 collide again. 194 00:12:10,170 --> 00:12:12,690 And then we can enable the landmine. 195 00:12:12,720 --> 00:12:14,490 I think that's all we need to do there. 196 00:12:14,790 --> 00:12:19,260 Now, back up here, I think it is time for us to. 197 00:12:19,260 --> 00:12:19,650 Ah, yes. 198 00:12:19,650 --> 00:12:21,480 Let's fill out these functions right here. 199 00:12:23,110 --> 00:12:27,160 So what do we want to happen when our land mine gets touched? 200 00:12:27,400 --> 00:12:32,350 Well, first off, we should check if the land mine has already been detonated. 201 00:12:32,350 --> 00:12:35,260 Because if it has, we don't want to detonate it again. 202 00:12:35,260 --> 00:12:40,090 So if self dot detonated, then we're going to return. 203 00:12:41,440 --> 00:12:47,350 Um, another thing we need to make sure of is that whatever part's touching, our landmine isn't anchored. 204 00:12:47,350 --> 00:12:51,790 We only want to look at unanchored parts, because when the game starts, the landmine is going to be 205 00:12:51,790 --> 00:12:56,140 touching the ground, and the ground's anchored and the ground has a lot of mass, so it's going to 206 00:12:56,140 --> 00:12:57,010 blow up the landmine. 207 00:12:57,010 --> 00:12:58,510 But we don't want that to happen. 208 00:12:58,510 --> 00:13:04,540 So we need to do, um, if this other part that touched this landmine is anchored, then we're going 209 00:13:04,540 --> 00:13:05,350 to ignore it, all right? 210 00:13:05,350 --> 00:13:06,460 We don't care about it. 211 00:13:06,460 --> 00:13:12,220 Otherwise we can get the weight of this part and any other parts that it may be part of in a model. 212 00:13:12,250 --> 00:13:16,810 By using this get total weight function, we're about to fill out, and we can return a number from 213 00:13:16,810 --> 00:13:17,320 this function. 214 00:13:17,320 --> 00:13:21,550 We can store it in a variable called weight, get total weight, and pass this other part. 215 00:13:23,370 --> 00:13:25,170 And let's go ahead and fill out this function. 216 00:13:25,170 --> 00:13:28,170 So what we could do is create a variable called total weight. 217 00:13:28,320 --> 00:13:30,030 Set it to nothing for now. 218 00:13:30,030 --> 00:13:36,930 And then we can check if this part um, or actually if the part parent is a model. 219 00:13:36,930 --> 00:13:39,600 So this part is part of a model. 220 00:13:39,600 --> 00:13:43,530 Then we can set total weights equal to zero. 221 00:13:44,250 --> 00:13:50,640 And we're going to loop through every single child and ipairs part dot parent. 222 00:13:50,640 --> 00:13:53,190 And we're going to get all of the descendants. 223 00:13:53,190 --> 00:13:56,190 So everything that is a part of this model. 224 00:13:57,280 --> 00:14:02,380 And what we'll do is we're going to make sure that this child is a base part. 225 00:14:02,380 --> 00:14:05,080 So if not, child is a. 226 00:14:06,550 --> 00:14:07,600 Bass part. 227 00:14:09,560 --> 00:14:12,140 Then we're going to continue looping. 228 00:14:12,680 --> 00:14:20,750 Otherwise we can set total weight plus equal to the child dot I believe it's mass. 229 00:14:20,750 --> 00:14:25,940 So all bass parts have a property called mass and we'll add it to the total weight. 230 00:14:26,920 --> 00:14:32,710 And then once this is done looping, what we're going to do is we're going to return the total weight. 231 00:14:34,370 --> 00:14:41,900 But let's say this part was by itself and it wasn't a parent or it wasn't a child of a model. 232 00:14:42,050 --> 00:14:44,930 We just want to pass the weight of the part instead. 233 00:14:44,930 --> 00:14:47,630 Instead of this total weight that never got instantiated here. 234 00:14:47,630 --> 00:14:51,230 So we could put an Or statement here and do part dot mass. 235 00:14:51,710 --> 00:14:57,440 So just in case total weight hasn't been instantiated yet because the part is not part of a model, 236 00:14:57,440 --> 00:15:00,680 then we'll just pass the parts mass just by itself. 237 00:15:01,100 --> 00:15:05,000 So now we get our number from this function stored in this variable here. 238 00:15:06,340 --> 00:15:13,240 And then we can check if the weight is greater than or equal to the self dot properties of this object, 239 00:15:13,240 --> 00:15:16,510 and the property is called weight sensitivity. 240 00:15:17,080 --> 00:15:21,400 So if it's greater than or equal to the weight sensitivity. 241 00:15:23,010 --> 00:15:26,580 Then we can blow up the landmine. 242 00:15:27,810 --> 00:15:28,890 Seems good. 243 00:15:29,430 --> 00:15:30,720 So we blow up the landmine. 244 00:15:30,720 --> 00:15:33,750 Landmine gets detonated only when it gets touched once. 245 00:15:33,990 --> 00:15:35,640 Um, we've initialized everything. 246 00:15:35,640 --> 00:15:42,900 I think the next thing we need to do is to fill out, um, for our explosion class, we got to make 247 00:15:42,900 --> 00:15:44,430 this constructor function. 248 00:15:44,430 --> 00:15:46,290 So let's go ahead and do that. 249 00:15:46,290 --> 00:15:50,520 Let's go into our explosion class and let's get working. 250 00:15:50,730 --> 00:15:53,730 So inside of our explosion class I don't think we'll need any services. 251 00:15:53,730 --> 00:15:55,230 We'll just need some variables. 252 00:15:55,230 --> 00:15:57,540 Again we're going to uphold encapsulation. 253 00:15:57,540 --> 00:16:01,380 We'll create two tables one for storing the constructor. 254 00:16:02,180 --> 00:16:04,610 And another to be the meta table for this class. 255 00:16:04,610 --> 00:16:05,930 So explosion empty. 256 00:16:05,960 --> 00:16:13,040 We'll call it, um, an explosion in Roblox needs to have a position. 257 00:16:13,280 --> 00:16:16,610 And the position will be, um, the landmine. 258 00:16:16,610 --> 00:16:16,910 Right? 259 00:16:16,910 --> 00:16:18,230 The position of the landmine. 260 00:16:18,230 --> 00:16:24,710 So we could pass the landmine to the constructor for this explosion, and we could store it in a property. 261 00:16:24,710 --> 00:16:25,970 We could just call it parent. 262 00:16:25,970 --> 00:16:29,300 We can set the explosions parent to this landmine. 263 00:16:29,300 --> 00:16:30,800 We'll set it to nil for now. 264 00:16:31,160 --> 00:16:37,850 And I think we'll also need to access the properties that are inside of our landmine part. 265 00:16:37,850 --> 00:16:39,950 So we'll set it to nil for now as well. 266 00:16:40,190 --> 00:16:46,340 And again make sure to set the underscore underscore index meta method to itself. 267 00:16:48,180 --> 00:16:51,780 And then from here we're going to need a few constants. 268 00:16:52,020 --> 00:16:58,530 So let's say just in case somebody forgot to fill out some of the properties here and the explosion 269 00:16:58,530 --> 00:17:01,170 properties or they didn't fill it out correctly. 270 00:17:01,170 --> 00:17:08,220 Well we either want to supply the properties for our explosion instance to be either these or we could 271 00:17:08,220 --> 00:17:09,390 set it to a default. 272 00:17:09,390 --> 00:17:11,400 So let's define some defaults. 273 00:17:12,130 --> 00:17:15,610 And we'll just set it to the world origin as default. 274 00:17:15,610 --> 00:17:18,910 So that's a new vector three at world origin. 275 00:17:19,240 --> 00:17:20,380 So good. 276 00:17:20,380 --> 00:17:26,500 Now we can create some private functions for this class to, you know, hide away unnecessary details. 277 00:17:27,410 --> 00:17:33,320 We'll have a function for creating the explosion, this explosion instance. 278 00:17:33,590 --> 00:17:36,530 And actually we'll need another function above it. 279 00:17:36,890 --> 00:17:45,290 I'm going to call it on explosion hit, because an explosion instance has a hit event, and it goes 280 00:17:45,290 --> 00:17:48,080 through and tells us all the parts that the explosion hit. 281 00:17:48,080 --> 00:17:54,110 And from that we can see if the part we hit belonged to a humanoid, and we can damage the humanoid 282 00:17:54,110 --> 00:17:55,550 accordingly to that. 283 00:17:55,550 --> 00:17:58,820 So we'll make sure to call this function on that event. 284 00:17:59,360 --> 00:18:03,560 Otherwise we'll just need some public functions like the constructor. 285 00:18:07,540 --> 00:18:12,220 So we'll do function explosion dot new. 286 00:18:12,640 --> 00:18:16,540 What do we need to pass to this constructor? 287 00:18:16,570 --> 00:18:22,090 Well of course we need the parent or the landmine. 288 00:18:22,090 --> 00:18:22,360 Right. 289 00:18:22,360 --> 00:18:25,150 The part that has the properties module script in it. 290 00:18:26,050 --> 00:18:30,460 Um, we should probably pass to this function. 291 00:18:31,070 --> 00:18:35,000 The amount of damage we want to do. 292 00:18:37,450 --> 00:18:39,460 And that'll be part of a number. 293 00:18:41,220 --> 00:18:42,690 We'll pass again. 294 00:18:42,690 --> 00:18:44,100 The blast pressure. 295 00:18:46,100 --> 00:18:47,810 The blast radius. 296 00:18:49,350 --> 00:18:54,660 All right, so now that we've got that done, um, we'll create a new table called Self Equal. 297 00:18:54,660 --> 00:18:59,370 To set the table, create a new table, attach explosion meta table to it. 298 00:19:01,000 --> 00:19:05,290 Uh, the self dot parent is going to be equal to the parent. 299 00:19:05,290 --> 00:19:06,370 And actually. 300 00:19:07,210 --> 00:19:08,050 Um. 301 00:19:08,530 --> 00:19:09,130 Never mind. 302 00:19:09,130 --> 00:19:10,600 We don't need to define it there. 303 00:19:10,600 --> 00:19:15,100 But we do need to have a damage property we can set. 304 00:19:15,100 --> 00:19:16,240 We'll set it to zero. 305 00:19:16,240 --> 00:19:20,230 That way, we know that we need a damage property right here. 306 00:19:20,350 --> 00:19:23,380 The damage is equal to the damage passed to this function. 307 00:19:24,310 --> 00:19:30,070 And actually, I think a better way that we could do this instead of having so many arguments we need 308 00:19:30,070 --> 00:19:31,930 to pass to the function. 309 00:19:32,290 --> 00:19:38,140 We could just set it up in here where all of these properties are actually a dictionary. 310 00:19:38,810 --> 00:19:40,340 So let's actually do that. 311 00:19:40,700 --> 00:19:45,260 Um, I'll create a new explosion instance here so you can see the names of the properties. 312 00:19:45,260 --> 00:19:46,790 Here's all the property names. 313 00:19:46,790 --> 00:19:48,770 Again this is blast pressure. 314 00:19:49,160 --> 00:19:52,640 Make sure you match all the capitalization. 315 00:19:52,640 --> 00:19:58,010 But I'm pretty sure I'll update this instance or the model attached to the lecture to include this new 316 00:19:58,010 --> 00:19:58,700 stuff. 317 00:19:58,790 --> 00:20:01,940 So blast pressure equal to 600,000. 318 00:20:01,970 --> 00:20:05,300 The blast radius equal to this. 319 00:20:05,570 --> 00:20:11,300 We will have the destroy joint radius percent. 320 00:20:12,450 --> 00:20:14,700 We're going to have. 321 00:20:15,450 --> 00:20:17,730 The explosion type. 322 00:20:19,270 --> 00:20:21,940 Well, let's actually make this table a little bigger. 323 00:20:22,090 --> 00:20:25,600 And then the last one here is going to be the time scale. 324 00:20:28,540 --> 00:20:31,030 And I don't think we need to store the position in here. 325 00:20:31,030 --> 00:20:35,230 We'll take care of that inside of our module script here. 326 00:20:35,230 --> 00:20:42,370 So instead what we could do is we could just have the player pass the properties table to us, and then 327 00:20:42,370 --> 00:20:43,960 we can just store that here. 328 00:20:44,580 --> 00:20:45,510 Instead. 329 00:20:45,510 --> 00:20:48,060 I think that's a lot simpler. 330 00:20:48,270 --> 00:20:48,480 Um. 331 00:20:48,480 --> 00:20:48,840 Oh, yeah. 332 00:20:48,840 --> 00:20:50,610 We also need to pass the damage. 333 00:20:51,360 --> 00:20:53,520 So we'll store the damage. 334 00:20:53,520 --> 00:20:59,220 We'll have the part that's going to be the parent of our explosion, the properties for the explosion. 335 00:20:59,220 --> 00:20:59,550 All right. 336 00:20:59,550 --> 00:21:00,540 All is good. 337 00:21:01,110 --> 00:21:03,300 So we set all of that up. 338 00:21:04,550 --> 00:21:06,800 And then I think that's actually it. 339 00:21:06,800 --> 00:21:08,450 We can just return self here. 340 00:21:08,450 --> 00:21:11,150 That's all we need to do for our explosion setup. 341 00:21:12,270 --> 00:21:16,590 And that unfortunately, means I have to get rid of all of my hard work for Constance. 342 00:21:16,590 --> 00:21:17,550 How sad. 343 00:21:17,580 --> 00:21:18,450 It's okay. 344 00:21:18,450 --> 00:21:22,080 I'll leave a note in the video so you don't have to fill that out by yourself. 345 00:21:22,560 --> 00:21:31,680 Anyways, another function we'll have for this class is we're going to have a function called explode. 346 00:21:31,680 --> 00:21:37,230 So when we want the our explosion object to blow up, we're going to blow it up. 347 00:21:37,230 --> 00:21:40,140 And that's what we're using the create explosion function for. 348 00:21:40,140 --> 00:21:41,940 And we'll pass self to it. 349 00:21:43,200 --> 00:21:45,660 And then we'll have a section for main. 350 00:21:46,280 --> 00:21:47,720 Which is just. 351 00:21:48,300 --> 00:21:52,740 Returning back the explosion table because this is a module script. 352 00:21:52,800 --> 00:21:57,150 And actually, let's make sure to do the same thing here because we haven't done that yet. 353 00:21:57,270 --> 00:22:02,070 Have a main section to return back the landmine table. 354 00:22:03,010 --> 00:22:06,340 So now we have our constructor created. 355 00:22:07,710 --> 00:22:11,280 Um, I guess we go ahead and pass these arguments to it. 356 00:22:11,280 --> 00:22:13,830 So here's our constructor right here. 357 00:22:13,830 --> 00:22:18,090 We need to pass the part which is going to be the part passed to this function. 358 00:22:18,090 --> 00:22:22,740 We need the damage which will be self dot properties dot damage. 359 00:22:22,890 --> 00:22:26,490 And I believe it is yeah damage per hit damage per hit. 360 00:22:26,880 --> 00:22:32,220 And then we need the properties for the explosion which will be stored in the module script. 361 00:22:32,220 --> 00:22:37,800 So its explosion properties self dot properties dot explosion properties. 362 00:22:37,800 --> 00:22:38,670 There we go. 363 00:22:39,030 --> 00:22:42,960 And we're going to get an explosive object back from this constructor. 364 00:22:44,760 --> 00:22:49,560 Now what do we want to do when this create explosion function is called? 365 00:22:49,590 --> 00:22:53,490 Well, what we need to do is to create an explosion instance. 366 00:22:53,490 --> 00:22:57,870 So explosion is equal to instance dot new explosion. 367 00:22:59,050 --> 00:23:05,920 And then from this point, we can loop through every single property and every single value that is 368 00:23:05,920 --> 00:23:08,830 in that properties table that's stored here. 369 00:23:08,830 --> 00:23:12,730 So every single key and every single value. 370 00:23:12,730 --> 00:23:15,370 So every single key, every value. 371 00:23:15,400 --> 00:23:16,780 We're going to loop through all of them. 372 00:23:16,780 --> 00:23:21,010 Set the property equal to this value in our explosion. 373 00:23:21,560 --> 00:23:24,020 And we can do that by looping through it. 374 00:23:24,020 --> 00:23:27,470 We'll make sure to pass Oh Self to this function. 375 00:23:27,950 --> 00:23:29,060 So. 376 00:23:29,990 --> 00:23:32,570 We do self dot properties. 377 00:23:34,510 --> 00:23:38,650 And then we can set explosion this property. 378 00:23:41,150 --> 00:23:42,500 Equal to the value. 379 00:23:42,500 --> 00:23:43,340 Perfect. 380 00:23:43,340 --> 00:23:45,440 So now our explosion has all the properties. 381 00:23:45,440 --> 00:23:48,110 We also need to set up, um one more property. 382 00:23:48,110 --> 00:23:50,480 We need to set the position. 383 00:23:50,480 --> 00:24:00,770 So explosion dot position is going to be equal to self dot parents C frame. 384 00:24:01,580 --> 00:24:02,690 That position. 385 00:24:04,090 --> 00:24:09,730 And then we need to listen to an event in our explosion called Hit Like That. 386 00:24:09,730 --> 00:24:15,490 And we're going to connect a Lambda function to it, get the part that this explosion hit. 387 00:24:15,490 --> 00:24:19,000 And we're going to call the on explosion hit function. 388 00:24:19,000 --> 00:24:21,700 That way we're able to pass self to it. 389 00:24:23,470 --> 00:24:27,250 So past self and the part that got hit in the explosion. 390 00:24:27,670 --> 00:24:33,190 And now to actually detonate the explosion, all we need to do is parent it to the workspace. 391 00:24:33,190 --> 00:24:40,990 So we could do explosion dot parent equal to the self dot parent property which is our landmine part. 392 00:24:40,990 --> 00:24:44,890 So it's going to go boom is what's going to happen. 393 00:24:45,430 --> 00:24:48,490 And then we could wait some arbitrary amount of time, like five seconds. 394 00:24:48,490 --> 00:24:52,420 So the explosion will be in the workspace for at least five seconds. 395 00:24:53,180 --> 00:24:54,950 And then we can do explosion. 396 00:24:54,950 --> 00:24:55,610 Destroy. 397 00:24:55,610 --> 00:24:57,140 So we'll destroy the explosion after. 398 00:24:57,140 --> 00:24:58,880 We don't need it that way. 399 00:24:58,880 --> 00:25:03,410 You know, we don't have a bunch of explosions piling up in the workspace. 400 00:25:03,500 --> 00:25:06,230 So now let's fill out the on explosion hit function. 401 00:25:06,920 --> 00:25:10,220 Um, what we could do is fill out the parameter section. 402 00:25:10,220 --> 00:25:14,480 Remember, we get the object and we get the part that was hit. 403 00:25:14,690 --> 00:25:18,920 And then what we could do is we could check if there is a humanoid that was a part of this part that 404 00:25:18,920 --> 00:25:19,310 got hit. 405 00:25:19,310 --> 00:25:26,360 So part dot parent find first child, which is a humanoid. 406 00:25:28,380 --> 00:25:30,540 If there is no humanoid, then we don't care. 407 00:25:30,540 --> 00:25:31,770 We'll just return end. 408 00:25:31,800 --> 00:25:33,330 Otherwise we can do humanoid. 409 00:25:33,360 --> 00:25:35,940 Take damage equal to self. 410 00:25:35,940 --> 00:25:36,900 Dot damage. 411 00:25:36,900 --> 00:25:37,830 Just like that. 412 00:25:38,560 --> 00:25:40,900 And I think that looks pretty good. 413 00:25:41,110 --> 00:25:43,210 So we've got this filled out. 414 00:25:43,240 --> 00:25:48,130 We pass the arguments we've needed to our constructor and then ah yes. 415 00:25:48,130 --> 00:25:53,770 One more thing we have to do is that when we get this explode function called from when our landmine 416 00:25:53,770 --> 00:26:03,280 gets touched, what we need to do is self dot explosive and call the explode function on it that exists 417 00:26:03,280 --> 00:26:06,700 here within our explosion class. 418 00:26:06,700 --> 00:26:12,640 So now what we need to do is run these module scripts by calling this initialize function. 419 00:26:13,500 --> 00:26:17,070 So this is where my server script comes into play. 420 00:26:17,870 --> 00:26:23,180 I'll just have one section for the services, which is to get the server storage. 421 00:26:25,370 --> 00:26:28,220 Because again, this is where my classes are stored. 422 00:26:28,780 --> 00:26:30,760 We're just going to need one variable. 423 00:26:32,260 --> 00:26:34,240 That's our landmine class. 424 00:26:34,240 --> 00:26:41,710 So we're going to require server storage dot classes folder and get my landmine class. 425 00:26:42,740 --> 00:26:46,280 And then finally we'll have a section called main. 426 00:26:46,280 --> 00:26:51,860 It's important to stay organized and we could do landmine init initialize. 427 00:26:51,860 --> 00:26:55,250 So let's start up everything that means. 428 00:26:56,190 --> 00:26:57,180 I think that means we're done. 429 00:26:57,180 --> 00:27:00,060 So the only way to know if we're done is to test it. 430 00:27:00,060 --> 00:27:00,480 Right? 431 00:27:00,480 --> 00:27:02,280 So let's go ahead and test it out. 432 00:27:02,880 --> 00:27:03,240 All right. 433 00:27:03,240 --> 00:27:04,170 No errors in the console. 434 00:27:04,170 --> 00:27:04,500 That's good. 435 00:27:04,500 --> 00:27:05,550 That's always a thumbs up. 436 00:27:06,820 --> 00:27:08,980 Now let's go and touch our landmine. 437 00:27:10,290 --> 00:27:11,700 And there we go. 438 00:27:12,510 --> 00:27:13,830 It blew up. 439 00:27:13,860 --> 00:27:19,080 Um, unfortunately, our landmine is not disappearing instantly, and I think I know why. 440 00:27:19,920 --> 00:27:22,080 So if we go to explosion. 441 00:27:22,080 --> 00:27:22,440 Right. 442 00:27:22,440 --> 00:27:25,740 So when we call the explode function, it calls this function. 443 00:27:25,740 --> 00:27:32,700 And because it's yielding, um, we aren't able to get to this section until it's done yielding. 444 00:27:32,700 --> 00:27:37,440 So what we could do is we could do task dot spawn spawn a lambda function here. 445 00:27:41,040 --> 00:27:48,120 And that way we're exploding in a separate thread, and these can get run at the exact same time as 446 00:27:48,120 --> 00:27:51,450 our, um, explosive object is exploding. 447 00:27:51,960 --> 00:27:53,700 So let's go ahead and try that again. 448 00:27:54,820 --> 00:27:57,640 So if we go and touch our landmine, boom! 449 00:27:57,640 --> 00:27:58,480 And it's disappeared. 450 00:27:58,480 --> 00:27:59,290 Perfect. 451 00:28:00,480 --> 00:28:04,230 And now that means we should be able to duplicate this as many times as we want. 452 00:28:04,410 --> 00:28:11,580 We can put these in whatever folders or parents we want, and each one of these things should be able 453 00:28:11,580 --> 00:28:12,150 to blow up. 454 00:28:12,150 --> 00:28:17,190 So if we go and touch one, boom blows up, we touch another, one, boom blows up and we're dead. 455 00:28:18,000 --> 00:28:18,300 All right. 456 00:28:18,300 --> 00:28:21,030 So congratulations on making it to the end of this lecture. 457 00:28:21,030 --> 00:28:25,860 I know it was an earful and a lot of information to absorb, but I hope you're learning a lot so far, 458 00:28:25,860 --> 00:28:30,660 and I hope this lecture has given you an example of how you can implement composition and the many other 459 00:28:30,660 --> 00:28:32,730 object oriented principles in your game. 460 00:28:32,730 --> 00:28:34,320 I'll see you in the next lecture.